'use strict';
app.controller('ImgCropCtrl', [
    '$scope',
    '$http', 'dialogs', '$state',
    function ($scope, $http, dialogs, $state) {
        $scope.myImage = '';
        $scope.myCroppedImage = '';
//			$scope.cropType = "square";

        var handleFileSelect = function (evt) {
            var file = evt.currentTarget.files[0];
            var reader = new FileReader();
            reader.onload = function (evt) {
                $scope.$apply(function ($scope) {
                    $scope.myImage = evt.target.result;
                });
            };
            reader.readAsDataURL(file);
            $("#upload").attr("disabled", false);
        };
        angular.element(document.querySelector('#fileInput')).on('change',
            handleFileSelect);

        /*function getBlobBydataURL(dataURI, type) {
         var binary = atob(dataURI.split(',')[1]);
         var array = [];
         for (var i = 0; i < binary.length; i++) {
         array.push(binary.charCodeAt(i));
         }
         return new Blob([ new Uint8Array(array) ], {
         type : type
         });
         }

         var $Blob = getBlobBydataURL($scope.myCroppedImage, "image/png");
         $scope.file = $Blob;*/

        $scope.submit = function () {
            return $http.post(
                ISS.getPath('/user/upload'),
                {'base64': $scope.myCroppedImage},
                {contentType: 'json'}
            ).then(function (resp) {
                if (resp.data.success === false) {
                    dialogs.notify(resp.data);
                } else {
                    dialogs.notify(resp.data).closed.then(function () {
                        $scope.app.loginUser.imageData = $scope.myCroppedImage; //设置右上角的图像
//                            	$state.go('setting.personal')
                    });
                }
            });
        }
    }]);